package com.idbk.chargestation.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationURL;
import com.idbk.chargestation.bean.JsonCertification;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 我的爱车适配器
 * @author liangzw,lupc
 *
 */
public class MyCarAdapter extends BaseAdapter {

	private Context mContext;
	private List<JsonCertification> list;
	private LayoutInflater mInflater;
	
	public MyCarAdapter(List<JsonCertification> list, Context context) {
		mContext = context;
		this.list = list;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class ViewHold {
		private ImageView imageCarIcon,imageCarStatus;
		private TextView textInfo, textNumber, textTime;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final JsonCertification msg = list.get(position);
		ViewHold item;
		if(convertView == null) {
			item = new ViewHold();
			convertView = mInflater.inflate(R.layout.item_my_car, parent, false);
			item.imageCarIcon = (ImageView) convertView.findViewById(R.id.imageview_car_certification_icon);
			item.imageCarStatus = (ImageView) convertView.findViewById(R.id.imageview_car_certification_state);
			
			item.textTime = (TextView) convertView.findViewById(R.id.textview_car_certification_time);
			item.textInfo = (TextView) convertView.findViewById(R.id.textview_car_certification_info);
			item.textNumber = (TextView) convertView.findViewById(R.id.textview_car_certification_number);
			convertView.setTag(item);
		} else {
			item = (ViewHold) convertView.getTag();
		}
		//获取图片资源
		String url = ChargeStationURL.getAbsoluteApiUrlFromRoot(msg.logoURL);
		Picasso.with(mContext)
		.load(url)
		.into(item.imageCarIcon);
		
		item.textTime.setText(msg.submitTime);
		
		item.textInfo.setText(msg.brandName + " " + msg.typeName);//车辆品牌车型
		item.textNumber.setText("车牌号:" + " " + msg.vehicleNum);//车牌号
		//审核状态
		if(msg.checkStatus == JsonCertification.STATE_CERTIFICATION_FAILD) {  //审核失败
			item.imageCarStatus.setImageResource(R.drawable.ico_verify_fault);

		} else if(msg.checkStatus == JsonCertification.STATE_CERTIFICATION_IN) {  //审核中
			item.imageCarStatus.setImageResource(R.drawable.ico_verify_in);

		} else if(msg.checkStatus == JsonCertification.STATE_CERTIFICATION_SUCCESS) {  //审核通过
			item.imageCarStatus.setImageResource(R.drawable.ico_verify_success);
		}
		return convertView;
	}
	
}
